package ThirdPagh;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SHU.water2.R;

import static SHU.water2.loginin.connection;
import static SHU.water2.loginin.nickname;

public class historyL extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    public ListView list;
    public String sql;
    public static String picUrl;
    private ArrayList<String> L1;
    private ArrayList<String> L2;
    private Handler handler;
    HashMap<String, Object> item;
    public List<HashMap<String, Object>> data;
    private boolean getdata = false;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.historylocation);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();
        handler = new Handler() {
            //接收消息
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                setView();
            }
        };
        list = findViewById(R.id.list1);
        sql = "select time,pic from history_location where name ='" + nickname + "'";
        connect(sql);
        click();

    }

    private void click() {
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.re).setOnClickListener(this);
        list.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ret1:
                finish();
                break;
            case R.id.re:
                if (getdata == false) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("尚未读回数据，请刷新重试").setPositiveButton("是", null).show();
                } else
                    setView();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // TODO Auto-generated method stub
        picUrl = L2.get(position);
        System.out.println(picUrl);
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), historypic.class);
        startActivity(intent);
    }

    private void connect(String sql) {
        L1 = new ArrayList<>();
        L2 = new ArrayList<>();
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 创建用来执行sql语句的对象
                    Statement statement = connection.createStatement();
                    ResultSet rSet = statement.executeQuery(sql);

                    while (rSet.next()) {
                        L1.add(rSet.getString("time"));
                        L2.add(rSet.getString("pic"));
                    }

                    data = new ArrayList<HashMap<String, Object>>();

                    for (int i = 0; i < L1.size(); i++) {
                        item = new HashMap<String, Object>();
                        item.put("time", L1.get(i));
                        item.put("pic", L2.get(i));

                        data.add(item);
                    }

                    if (L1 != null) {
                        getdata = true;
                    }
                    Message msg = new Message();
                    handler.sendMessage(msg);

                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void setView() {
        //创建SimpleAdapter适配器将数据绑定到item显示控件上
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.historykocationlist,
                new String[]{"time"}, new int[]{R.id.time});

        ListView listView = (ListView) findViewById(R.id.list1);
        //实现列表的显示
        listView.setAdapter(adapter);
    }
}